<template>
  <div class="home">
    <van-search
      v-model="value"
      shape="round"
      background="#ff5000"
      placeholder="寻找宝贝店铺"
      show-action
      @focus="goDetail('/goods/goodsfind', value)"
    >
      <template #action>
        <van-icon
          name="https://b.yzcdn.cn/vant/icon-demo-1126.png"
          @click="goDetail('/login', '')"
        />
      </template>
    </van-search>
    <Swiper :lunbolist="lunbolist" />
    <back-top />
    <van-grid>
      <van-grid-item
        v-for="(item, index) in grids"
        :icon="item.image_src"
        :key="item.id"
        @click="goHere(index)"
      />
    </van-grid>
    <div
      class="commodity"
      v-for="item in commodity"
      :key="item.floor_title.name"
    >
      <img class="banner" :src="item.floor_title.image_src" alt="" />
      <div class="product clear_fix">
        <img
          v-for="pic in item.product_list"
          v-lazy="pic.image_src"
          :key="pic.name"
          alt=""
          @click="goGoods(pic.navigator_url)"
        />
      </div>
    </div>
    <p>---------- 已经到底啦 (*￣︶￣) ----------</p>
  </div>
</template>
<script>
import { getLunbo, getGrids, getCommodity } from '@/api/home'
import Swiper from '@/components/Swiper'
import backTop from '@/components/backTop'
import { Toast } from 'vant'
export default {
  data() {
    return {
      value: '',
      lunbolist: [],
      grids: [],
      commodity: []
    }
  },
  components: {
    Swiper,
    backTop
  },
  methods: {
    async getLunbo() {
      try {
        const res = await getLunbo()
        this.lunbolist = res.data.message
        console.dir(res.data.message)
      } catch (error) {
        Toast(error)
      }
    },
    async getGrids() {
      try {
        const res = await getGrids()
        this.grids = res.data.message
      } catch (error) {
        Toast(error)
      }
    },
    async getCommodity() {
      try {
        const res = await getCommodity()
        this.commodity = res.data.message
      } catch (error) {
        Toast(error)
      }
    },
    goHere(index) {
      if (index === 0) {
        this.$router.push('/itemize')
      }
      if (index === 2) {
        this.$router.push('/goods')
      }
    },
    goGoods(str) {
      const arr = str.split('=')
      console.log(arr[1])
      this.$router.push('/itemize' + '/' + arr[1])
    }
  },
  created() {
    this.getLunbo()
    this.getGrids()
    this.getCommodity()
  }
}
</script>
<style lang="scss">
* {
  margin: 0;
  padding: 0;
}
.clear_fix:after {
  content: ".";
  clear: both;
  height: 0;
  overflow: hidden;
  display: block;
  visibility: hidden;
}
.clear_fix {
  zoom: 1;
}
.home {
  .van-search {
    width: 100%;
    position: fixed;
    left: 0;
    top: 0;
    display: flex;
    align-content: center;
    z-index: 1000;
    .van-search__action {
      padding: 0 8px;
      color: #323233;
      font-size: 14px;
      line-height: inherit;
      cursor: pointer;
      -webkit-user-select: none;
      user-select: none;
      .van-icon__image {
        width: 2em;
        height: 2em;
      }
    }
    // .login {
    //   color: #fff;
    // }
  }
  position: relative;
  top: -46px;
  .van-grid-item__icon {
    font-size: 68px;
  }
  .commodity {
    margin-bottom: 10px;
    .banner {
      width: 100%;
      height: 50px;
    }
    .product {
      flex-direction: column;
      background: #f4f4f4;
      img {
        margin: 4px 4px;
        float: left;
        width: 47%;
      }
    }
  }
  p {
    margin-top: 23px;
    text-align: center;
    color: #999999;
  }
}
</style>